

//
//  ZYResourceSSSSController.m
//  优学荟管理系统
//
//  Created by edz on 2017/11/8.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "ZYResourceSSSSController.h"
#import "ZYResourceSSSSCell.h"
#import "ZYResourceSSSSModel.h"
#define kCell @"cell"
#import "HNSalesManCycleDetailDateView.h"
@interface ZYResourceSSSSController ()<UITableViewDelegate,UITableViewDataSource,CustomAlertDelegete>
{
    
    NSInteger _mark;//标记类型
    NSString *_selectDate;//标记选中的日期
    
}
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIView *allView;
@property (nonatomic,assign) BOOL isTrue;
@property (nonatomic,assign) NSInteger page;
@property (nonatomic,strong) NSMutableArray *dataSource;

@property (nonatomic,copy) NSString *startTime;
@property (nonatomic,copy) NSString *endTime;

@property (nonatomic,strong) UILabel *startLabel;//开始时间
@property (nonatomic,strong) UIButton *startSelectButton;//选择开始时间
@property (nonatomic,strong) UILabel *endLabel;//结束时间
@property (nonatomic,strong) UIButton *endSelectButton;//选择结束时间

@property (nonatomic,strong) UIButton *sureButton;//确定

@property (nonatomic,strong) UILabel *timeLabel;//
@property (nonatomic,strong) UILabel *moneyLabel;//





@property (nonatomic,strong) CustomDayDatePicker *datePicker;

@property (nonatomic,assign) BOOL isSure;


@end

@implementation ZYResourceSSSSController

- (NSMutableArray *)dataSource{
    
    if (_dataSource == nil) {
        self.dataSource = [NSMutableArray array];
    }
    
    return _dataSource;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"结算历史";
    [self setUpSuperView];
    [self setUpRefresh];
}


- (CustomDayDatePicker *)datePicker {
    
    if (_datePicker == nil) {
        self.datePicker = [[CustomDayDatePicker alloc]initWithFrame:CGRectMake(0, 20, kScreenWidth - 60, 140)];
        
    }
    
    
    return _datePicker;
}



- (void)setUpSuperView{
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64)];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    self.tableView.estimatedRowHeight = 200;
    
    
    [self.tableView registerClass:[ZYResourceSSSSCell class] forCellReuseIdentifier:kCell];
    self.tableView.separatorStyle =  UITableViewCellSeparatorStyleNone;
//    self.tableView.contentInset = UIEdgeInsetsMake(-54, 0, 0, 0);
    
    
    
    self.allView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth,220)];
    
    self.tableView.tableHeaderView = self.allView;
    
    self.startLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, kScreenWidth - 140, 20)];
    self.startLabel.text = @"开始时间";
    
    self.startLabel.textColor = ColorRGB(127, 125, 125);
    self.startLabel.font = [UIFont systemFontOfSize:14];
    [self.allView addSubview:self.startLabel];
    
    
    self.startSelectButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.startSelectButton.frame = CGRectMake(kScreenWidth - 140,self.startLabel.frame.origin.y,120,20);
    [self.startSelectButton setImage:[UIImage imageNamed:@"icon_time_default"] forState:(UIControlStateNormal)];
    [self.startSelectButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [self.startSelectButton setTitle:[NSString stringWithFormat:@"%@",[NSString getCurrentDateYearMD]] forState:(UIControlStateNormal)];
    self.startSelectButton.titleLabel.font = [UIFont systemFontOfSize:14];
    self.startSelectButton.backgroundColor = ColorRGB(235, 250, 243);
    [self.allView addSubview:self.startSelectButton];
    
    
    self.endLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.startLabel.frame.origin.x,self.startLabel.frame.origin.y + self.startLabel.frame.size.height + 15,self.startLabel.frame.size.width , self.startLabel.frame.size.height)];
    self.endLabel.text = @"结束时间";
    self.endLabel.textColor = ColorRGB(127, 125, 125);
    self.endLabel.font = [UIFont systemFontOfSize:14];
    [self.allView addSubview:self.self.endLabel];
    
    self.endSelectButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.endSelectButton.frame = CGRectMake(self.startSelectButton.frame.origin.x,self.endLabel.frame.origin.y,self.startSelectButton.frame.size.width,self.startSelectButton.frame.size.height);
    [self.endSelectButton setImage:[UIImage imageNamed:@"icon_time_default"] forState:(UIControlStateNormal)];
    [self.endSelectButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    
    
    [self.endSelectButton setTitle:[NSString stringWithFormat:@"%@",[NSString yearMonthDay]] forState:(UIControlStateNormal)];

    self.startTime = [self MgrTimeStrFormatterWitStr:[NSString yearMonthDay]];
    self.endTime = [self MgrTimeStrFormatterWitStr:[NSString yearMonthDay]];
    
    self.endSelectButton.titleLabel.font = [UIFont systemFontOfSize:14];
    self.endSelectButton.backgroundColor = ColorRGB(235, 250, 243);
    [self.allView addSubview:self.endSelectButton];
    
    
    self.sureButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.sureButton.frame = CGRectMake(30, self.endSelectButton.frame.origin.y + self.endSelectButton.frame.size.height  + 30, kScreenWidth - 60, 30);
    [self.sureButton setTitle:@"确定" forState:(UIControlStateNormal)];
    
    [self.sureButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    self.sureButton.backgroundColor = ColorRGB(49, 204, 131);
    self.sureButton.layer.cornerRadius = 15;
    self.sureButton.clipsToBounds = YES;
    [self.allView addSubview:self.sureButton];
    UILabel *lineLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, self.sureButton.frame.origin.y + self.sureButton.frame.size.height + 20, kScreenWidth, 10)];
    lineLabel.backgroundColor = ColorRGB(245, 245, 245);
    [self.allView addSubview:lineLabel];
    
    
    
    self.timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, lineLabel.frame.origin.y + lineLabel.frame.size.height + 15, kScreenWidth/2, 20)];
    self.timeLabel.text = @"时间";
    self.timeLabel.textColor = ColorRGB(127, 125, 125);
    self.timeLabel.font = [UIFont systemFontOfSize:14];
    self.timeLabel.textAlignment = NSTextAlignmentCenter;
    [self.allView addSubview:self.timeLabel];
    
    self.moneyLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.timeLabel.frame.origin.x +self.timeLabel.frame.size.width,self.timeLabel.frame.origin.y, self.timeLabel.frame.size.width,self.timeLabel.frame.size.height)];
    self.moneyLabel.text = @"金额";
    self.moneyLabel.textColor = ColorRGB(127, 125, 125);
    self.moneyLabel.font = [UIFont systemFontOfSize:14];
    self.moneyLabel.textAlignment = NSTextAlignmentCenter;
    [self.allView addSubview:self.moneyLabel];
    
    
    UILabel *lineTowLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, self.moneyLabel.frame.origin.y + self.moneyLabel.frame.size.height + 15, kScreenWidth, 1)];
    lineTowLabel.backgroundColor = ColorRGB(245, 245, 245);
    [self.allView addSubview:lineTowLabel];
    
    
    
    self.startSelectButton.tag = 201;
    self.endSelectButton.tag = 202;
    [self.startSelectButton addTarget:self action:@selector(allButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.endSelectButton addTarget:self action:@selector(allButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.sureButton addTarget:self action:@selector(sureButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    
}

#pragma mark ---详情方法的实现
- (void)allButtonAction:(UIButton *)sender{
    
    NSInteger tag = sender.tag;
    
    if (201 == tag) {
        
        NSLog(@"dd");
        
        JXAlertview *alert = [[JXAlertview alloc]initWithFrame:CGRectMake(10, 0, kScreenWidth - 60, 200)];
        alert.center = self.view.center;
        alert.delegate = self;
        [alert initwithtitle:@"开始时间" andcommitbtn:@"确定" andStr:@"0"];
        [alert addSubview:self.datePicker];
        [alert show];
        
        
        _mark = 1;
        
        
        
    }else if (202 == tag){
        
        JXAlertview *alert = [[JXAlertview alloc]initWithFrame:CGRectMake(10, 0, kScreenWidth - 60, 200)];
        alert.center = self.view.center;
        alert.delegate = self;
        [alert initwithtitle:@"结束时间" andcommitbtn:@"确定" andStr:@"0"];
        [alert addSubview:self.datePicker];
        [alert show];
        
        
        _mark = 2;
        
        
    }
    
    
    
    
    
}

- (void)btnindex:(int)index :(int)tag{
    
    if (index == 2) {
        if (_mark == 1) {
            _selectDate = [NSString stringWithFormat:@"%d-%02d-%02d",self.datePicker.year,self.datePicker.month,self.datePicker.day];
            [self.startSelectButton setTitle:[NSString stringWithFormat:@"%@",_selectDate] forState:(UIControlStateNormal)];
        }else if (_mark == 2){
            
            _selectDate = [NSString stringWithFormat:@"%d-%02d-%02d",self.datePicker.year,self.datePicker.month,self.datePicker.day];
            [self.endSelectButton setTitle:[NSString stringWithFormat:@"%@",_selectDate] forState:(UIControlStateNormal)];
        }
    }
    
    
    
    
}



#pragma mark --- 上拉加载和下拉刷新方法的实现
- (void)setUpRefresh{
    
    //下拉刷新
    self.tableView.mj_header  = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(downRefresh)];
    //自动更改透明度
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    //进入刷新状态
//    [self.tableView.mj_header beginRefreshing];
    
    //上拉加载
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(upRefresh)];
//    //结束头部刷新
    [self.tableView.mj_header endRefreshing];
    //结束尾部刷新
    [self.tableView.mj_footer endRefreshing];
    
}

/**
 *  上拉加载
 */
- (void)upRefresh{
    
    self.isTrue = NO;
    self.page++;
    [self setNetWork];
    
}
/**
 *  下拉刷新
 */

- (void)downRefresh{
    self.isTrue = YES;
    self.page = 1;
    [self setNetWork];
    
    
}


- (void)setNetWork{
    
   // http://192.168.0.113:8080/StarShine/ys/resource.do?action=userSettle&uid=118270&startTime=2017-08&endTime=2017-11&pageNumber=1
    
    
//    NSString *url;
//    if (self.isSure) {
//
//        url  = [NSString stringWithFormat:@"resource.do?action=userSettle&uid=%@&startTime=%@&endTime=%@&pageNumber=%ld",self.ID,self.startTime,self.endTime,(long)self.page];
//    }else{
//
//        url = [NSString stringWithFormat:@"resource.do?action=userSettle&uid=%@&startTime=%@&endTime=%@&pageNumber=%ld",self.ID,self.startTime,self.endTime,(long)self.page];
//
//    }
    
    
    
    NSString *url  = [NSString stringWithFormat:@"resource.do?action=userSettle&uid=%@&startTime=%@&endTime=%@&pageNumber=%ld",self.ID,self.startTime,self.endTime,(long)self.page];
    
    
    
    
    
    
    NSLog(@"%@  %@",LLBaseURL(url),[AccountTool shareAccountTool].currentAccount.authCode);
    [SVProgressHUD setDefaultStyle:(SVProgressHUDStyleCustom)];
    [SVProgressHUD  setBackgroundColor:ColorRGB(4, 4, 4)];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    [SVProgressHUD showWithStatus:LLLoading];
    
    
    
    [NetManager Get:LLBaseURL(url) Auth:YES Params:nil Success:^(id Obj) {
        
        [SVProgressHUD dismiss];
        if ([[Obj valueForKey:@"code"] integerValue] == 1000) {
            
            if (self.isTrue == YES) {
                [self.dataSource removeAllObjects];
            }
            NSDictionary *t = [Obj valueForKey:@"t"];

            NSArray *arr = [t valueForKey:@"dataList"];
            
            
            for (NSDictionary *dict  in arr) {
                
                ZYResourceSSSSModel *model = [ZYResourceSSSSModel new];
                
                [model setValuesForKeysWithDictionary:dict];
                
                [self.dataSource addObject:model];
                
            }
            
            
            [self.tableView reloadData];
            
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            
        }
        
    } Fail:^(NSError *Error) {
        [SVProgressHUD dismiss];
    }];
    
    
    
    
    
}




#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ZYResourceSSSSCell *cell = [tableView dequeueReusableCellWithIdentifier:kCell forIndexPath:indexPath];
    
    if (self.dataSource.count > indexPath.row) {

        ZYResourceSSSSModel *model = self.dataSource[indexPath.row];

        [cell assignByZYResourceSSSSModel:model];

    }

    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    
    return cell;
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return 50;
}
#pragma mark --- 确定方法的实现
- (void)sureButtonAction:(UIButton *)sender{
    
    NSComparisonResult result = [self.startSelectButton.currentTitle compare:self.endSelectButton.currentTitle];
    
    if (result != NSOrderedAscending) {
        [SVProgressHUD showErrorWithStatus:@"结束时间必须大于开始时间!"];
    }else{
        
        NSString *start = [self.startSelectButton.currentTitle stringByReplacingOccurrencesOfString:@" " withString:@""];
        
        NSString *endSelect = [self.endSelectButton.currentTitle stringByReplacingOccurrencesOfString:@" " withString:@""];
        
        
        [self setSetTowSureStart:[self MgrTimeStrFormatterWitStr:start] endSelect:[self MgrTimeStrFormatterWitStr:endSelect]];
        
    }
    
//    if (![self JudgeBigOrSmallWithBegin:self.startSelectButton.currentTitle End:self.endSelectButton.currentTitle]) {
//
//        [SVProgressHUD showErrorWithStatus:@"结束时间必须大于开始时间!"];
//    } else {
//        // 发起时间查询的请求
//
//
//        NSString *start = [self.startSelectButton.currentTitle stringByReplacingOccurrencesOfString:@" " withString:@""];
//
//        NSString *endSelect = [self.endSelectButton.currentTitle stringByReplacingOccurrencesOfString:@" " withString:@""];
//
//
//        [self setSetTowSureStart:[self MgrTimeStrFormatterWitStr:start] endSelect:[self MgrTimeStrFormatterWitStr:endSelect]];
//
//
//    }
    
    
    
}


- (void)setSetTowSureStart:(NSString *)startStr endSelect:(NSString *)endSelect{
    
    
    self.startTime = startStr;
    self.endTime = endSelect;
//    self.isSure = YES;
//    self.isSearch = NO;
    
    [self.tableView.mj_header beginRefreshing];
    
    
    
    
    
}




/** 将2017年10月的格式转成2017-10 */
- (NSString *)MgrTimeStrFormatterWitStr:(NSString *)Str {
    
    Str = [Str stringByReplacingOccurrencesOfString:@"年" withString:@"-"];
    Str = [Str stringByReplacingOccurrencesOfString:@"月" withString:@""];
    return Str;
}








/** 判断结束时间是不要大于开始时间 */
- (BOOL)JudgeBigOrSmallWithBegin:(NSString *)Begin End:(NSString *)End {
    
    NSArray *BeginArr = [Begin componentsSeparatedByString:@"年"];
    NSString *BeginMouths = [[BeginArr lastObject] stringByReplacingOccurrencesOfString:@"月" withString:@""];
    NSArray *EndArr = [End componentsSeparatedByString:@"年"];
    NSString *EndMouths = [[EndArr lastObject] stringByReplacingOccurrencesOfString:@"月" withString:@""];
    NSInteger BeginYear = [[BeginArr firstObject] integerValue];
    NSInteger BeginMouth = [BeginMouths integerValue];
    NSInteger EndYear = [[EndArr firstObject] integerValue];
    NSInteger EndMouth = [EndMouths integerValue];
    if (EndYear - BeginYear > 0) {
        
        return YES;
    }
    else if (EndYear - BeginYear == 0 && EndMouth - BeginMouth >= 0) {
        
        return YES;
    } else {
        
        return NO;
    }
}


- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    
    [SVProgressHUD dismiss];
    
    
    
}









@end
